# calculator.py
class Calculator:
    """简单计算器类，支持加减乘除四则运算"""
    
    def __init__(self):
        """初始化计算器"""
        self.history = []  # 运算历史记录
    
    def add(self, a, b):
        """加法运算"""
        result = a + b
        self.history.append(f"{a} + {b} = {result}")
        return result
    
    def subtract(self, a, b):
        """减法运算"""
        result = a - b
        self.history.append(f"{a} - {b} = {result}")
        return result
    
    def multiply(self, a, b):
        """乘法运算"""
        result = a * b
        self.history.append(f"{a} × {b} = {result}")
        return result
    
    def divide(self, a, b):
        """除法运算，处理除数为零的情况"""
        if b == 0:
            self.history.append(f"错误：{a} ÷ {b}（除数不能为零）")
            raise ValueError("除数不能为零")
        result = a / b
        self.history.append(f"{a} ÷ {b} = {result}")
        return result
    
    def get_history(self):
        """获取运算历史"""
        return self.history

# 主程序
if __name__ == "__main__":
    calc = Calculator()
    
    while True:
        print("\n===== 简单计算器 =====")
        print("1. 加法")
        print("2. 减法")
        print("3. 乘法")
        print("4. 除法")
        print("5. 查看历史记录")
        print("0. 退出")
        
        choice = input("请选择操作(0-5): ")
        
        if choice == '0':
            print("感谢使用计算器！")
            break
            
        if choice not in ['1', '2', '3', '4', '5']:
            print("无效选择，请重试")
            continue
            
        if choice == '5':
            history = calc.get_history()
            if not history:
                print("暂无历史记录")
            else:
                print("\n--- 运算历史 ---")
                for i, item in enumerate(history, 1):
                    print(f"{i}. {item}")
            continue
            
        try:
            num1 = float(input("请输入第一个数字: "))
            num2 = float(input("请输入第二个数字: "))
            
            if choice == '1':
                print(f"结果: {calc.add(num1, num2)}")
            elif choice == '2':
                print(f"结果: {calc.subtract(num1, num2)}")
            elif choice == '3':
                print(f"结果: {calc.multiply(num1, num2)}")
            elif choice == '4':
                print(f"结果: {calc.divide(num1, num2)}")
                
        except ValueError as e:
            print(f"输入错误: {e}")
        except Exception as e:
            print(f"发生错误: {e}")